import { defineStore } from "pinia";

export const useStore = defineStore('user',{
    id: "user", // 必须指明唯一的pinia仓库的id
    state: () => ({
        name: "小明",
        address: "上海",
        age: 18,
        hobby: ["唱", "跳", "rap"],
    }),
    actions: {
        // actions 中可通过 this 来获取state中的数据
        // val 调用时时传过来的数据
        updateName(val) {
            this.name = this.name + val;
        },
        syncUpdateHobby(val) {
            setTimeout(() => {
                this.hobby.push(val);
            },100);
        },
    },
    getters: {
        getName(state) {
            // 方式1
            // return state.name
            // 方式2
            return this.name;
        },
    },
    persist: {
        enabled: true, // 启用持久化插件
		strategies: [
			{ storage: sessionStorage, paths: ['name', 'address'] }, // firstName 和 lastName字段用sessionStorage存储
			{ storage: localStorage, paths: ['hobby'] }, // accessToken字段用 localstorage存储
		  ],
    },
});
